/**
 * Created by CHENCO7 on 8/17/2017.
 */
function merchantDisplay(templateOfCategories, templateOfMerchants) {
    var self = this;
    this.modelOfCategories = null;
    this.modelOfMerchants = null;
    this.modelOfAdvertisement = null;
    this.templateOfCategories = templateOfCategories;
    this.templateOfMerchants = templateOfMerchants;
    /*获取菜品种类*/
    this.findAllCategories = function () {
        j.ajax("GET", "categories", null, null, this,
            function (categories) {
                this.modelOfCategories = categories;
                _render_Categories(this.modelOfCategories);
            });
    };
    /*根据菜品种类和商家id获取商家爱列表*/
    this.findMerchants = function (typeId) {
        j.ajax("GET", "getMerchants?typeId=" + typeId, null,
            null, this, function (merchants) {
                console.log(merchants);
                this.modelOfMerchants = merchants;
                _render_Merchants(this.modelOfMerchants);
            });
    };

    this.findAdvertisement = function () {
        j.ajax("GET", "http://10.222.29.159:8080/spring-admin/api/customer/ads", null,
            null, this, function (advertisementList) {
                console.log("tst..");
                this.modelOfAdvertisement = advertisementList;
                _render_advertisement(this.modelOfAdvertisement);
            });
    }

    function _render_advertisement(data) {
        $("#myCarousel").empty();
        var i = 0;
        data.forEach(function (item, index) {
            if (i == 0) {
                $("#myCarousel").append($('<div class="item active"><a href="merchantDetail?id=' + item.mid + "&foodId=" + item.foodId + '"><img src="' + item.imgPath + '" class="advertisement" alt="..."></a>'
                    + '<div class="carousel-caption"></div></div>'));
            }
            else {
                $("#myCarousel").append($('<div class="item"><a href="merchantDetail?id=' + item.mid + "&foodId=" + item.foodId + '"><img src="' + item.imgPath + '" class="advertisement" alt="..."></a>'
                    + '<div class="carousel-caption"></div></div>'));
            }
            i++;
        });
    }

    function _render_Categories(data) {
        templateOfCategories.empty();
        $("#category-list")
            .append(
                $(
                    "<li value='all' id='category-type-per' class='active'  role='presentation'></li>")
                    .append(
                        $(
                            "<a href='javascript:void(0);'></a>")
                            .text("All")));
        data
            .forEach(function (item) {
                $("#category-list")
                    .append(
                        $(
                            "<li value='" + item.id + "' id='category-type-per' role='presentation'></li>")
                            .append(
                                $(
                                    "<a href='javascript:void(0);'></a>")
                                    .text(item.type)));
            });
        $("li#category-type-per").click(function () {
            $(this).siblings().filter(".active").removeClass("active");
            $(this).addClass("active");
            console.log($(this).attr("value"));
            self.findMerchants($(this).attr("value"));
        });
    }

    function _render_Merchants(data) {
        templateOfMerchants.empty();
        data.forEach(function (item) {
            var clone = $("#merchant-model").clone().removeClass(
                "hidden-merchant-pattern");
            clone.children("div:nth-child(1)").children("div:nth-child(1)")
                .children("img").attr("src", item.avatarPath);
            clone.children("div:nth-child(1)").children("div:nth-child(2)")
                .children("h3").text(item.name);
            clone.children("div:nth-child(1)").children("div:nth-child(2)")
                .children("p").text(item.address);
            clone.children("div:nth-child(1)").children("div:nth-child(2)")
                .children("a").attr("value", item.id);
            /*  var div1 = $("<div id='merchant-model' class='show-merchant-list'></div>");
             var div2 = $("<div class='thumbnail'></div>");
             var a1 = $("<a id='merchant-img' href='javascript:void(0);' class='thumbnail'></a>");
             var img = $("<img id='merchant-image-show'  src='" + "http://ZHA-ITA077-w7/"+item.avatarPath + "' alt='" + item.name + "'>");
             var div3 = $("<div class='caption'></div>");
             var h3_name = $("<h3></h3>").text(item.name);
             var p_address = $("<p></p>").text(item.address);
             var a_detail = $("<a href='javascript:void(0);' class='btn btn-primary btn-lg show-merchant-detail' role='button'  value='" + item.id + "'>Detail</a>");
             */
            /* div3.append(h3_name).append(p_address).append(a_detail);
             div1.append(div2.append(a1.append(img).append(div3)));
             templateOfMerchants.append(div1);*/
            /*点击跳转到详情页*/
            clone.children("div:nth-child(1)").children("div:nth-child(2)")
                .children("a").on(
                "click",
                function () {
                    window.location.assign("merchant?id="
                        + $(this).attr("value"));
                });
            templateOfMerchants.append(clone);
        });
    }
}